package com.wxy.care.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxy.care.KnnUtils.Instance;
import com.wxy.care.KnnUtils.KNN;
import com.wxy.care.common.R;
import com.wxy.care.entity.Count;
import com.wxy.care.entity.CountDto;
import com.wxy.care.entity.Sensor;
import com.wxy.care.mapper.CountMapper;
import com.wxy.care.service.CountService;
import com.wxy.care.service.SensorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping
public class CountController {
    @Autowired
    private CountService countService;
    @Autowired
    private SensorService sensorService;
    @Autowired
    private CountMapper countMapper;

//    @RequestMapping("/predict")
//    public R<String>predict(String houseName){
//        if(houseName == null && houseName.equals(""))
//            return R.error("失败");
//        LambdaQueryWrapper<Sensor> queryWrapper = new LambdaQueryWrapper();
//        queryWrapper.eq(Sensor::getHouseName,houseName);
//        List<Sensor> sensors = sensorService.list(queryWrapper);
//        //System.out.println(sensors);
//        String prepare = countService.prepare(sensors);
//        return R.success(prepare);
//    }
    @RequestMapping("/count")
    public R<String>counter(String houseName,String slaveId,Integer status){
        //System.out.println(houseName + " --- " + slaveId);
        if(slaveId == null || "".equals(slaveId))
            return R.error("上传失败!");
        try {
            Count count = new Count();
            count.setDate(LocalDateTime.now());
            count.setHouseName(houseName);
            count.setSlaveId(slaveId);
            if(status != null)
                count.setStatus(status);
            countService.save(count);
        }catch (DataIntegrityViolationException e){
            //e.printStackTrace();
            return R.error("未知设备!上传失败!");
        }
        return R.success("上传成功");
    }
    @RequestMapping("/count/all")
    public R<List<Count>> countAll(){
        return R.success(countService.list());
    }

    @GetMapping("/getdata")
    public List<JSONObject> getData(String houseName){
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime preTime = localDateTime.minusMinutes(60);
        Map<String,Integer> countMap = new TreeMap<>();
        countMapper.getData(houseName,preTime).forEach(item->{
            String place = item.getPlaceStr();
            if(countMap.containsKey(place)){
                countMap.put(place,countMap.get(place) + 1);
            }
            else
                countMap.put(place,1);
        });
        List<JSONObject>data = new ArrayList<>();

        for(String i : countMap.keySet()){
            JSONObject js = new JSONObject();
            js.put("name",i);
            js.put("value",countMap.get(i));
            data.add(js);
        }
        List<JSONObject> series = new ArrayList<>(data);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("series",series);
        return series;
    }
    //最近一次出现的位置返回
    @GetMapping("/getstatus")
    public R<String>getFinalStatus(String houseName){
        LambdaQueryWrapper<Count> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Count::getHouseName,houseName)
                .orderByDesc(Count::getDate)
                .last("limit 1");
        Count count = countService.getOne(queryWrapper);
        if(count != null){
            LambdaQueryWrapper<Sensor> queryWrapper1 = new LambdaQueryWrapper();
            queryWrapper1.eq(Sensor::getHouseName,count.getHouseName())
                    .eq(Sensor::getSlaveId,count.getSlaveId());
            Sensor sensor = sensorService.getOne(queryWrapper1);
            return R.success(sensor.getPlaceStr());
        }
        return R.error("获取失败，当前无数据");
    }
}

